/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Reports.GenHTMLReport;

import java.io.File;

/**
 *
 * @author leng
 */
public class PrepareFolder {

    
    public PrepareFolder(){        
    }

    /**To a given folder name or path, check its existing and build it if necessary.
     * @param type
     *        1:    this directory is existing under current working folder.
     *        2:    this directory is a absolutely path.
     *
     * @return
     *          1:                  past the test or built the new folder successfully
     *          smaller than 0:     error occurred
     */
    public int check(String strDirectoy, int type){

        // ##1## check the validity of the directory Name
        if((strDirectoy==null)||(strDirectoy.trim().length()<1))
                return -2;

        // ##2## do action by the type
        switch(type){

            // ##2.1## directoryName is a small directory Name with
            case 1: {
                        try{
                                
                                File f = new File(strDirectoy);
                                if(f.exists()){
                                    System.out.println("Directories: "+ strDirectoy + " already existed.");

                                    // if existing and is a file, delete the file
                                    if(f.isFile()){
                                        boolean success_del = f.delete();
                                        if(success_del){

                                            // create the new folder
                                            boolean success = (new File(strDirectoy)).mkdir();
                                            if (success)
                                                System.out.println("Directories: " + strDirectoy + " created.");
                                            else
                                                System.out.println("Directories: " + strDirectoy + " could NOT be created.");
                                        }else{
                                            return -3;
                                        }
                                    
                                    }else{
                                        // if existing and is a directory, delete the files under it
                                        File delFile[] = f.listFiles();
                                        int i =f.listFiles().length;
                                        for(int j=0;j<i;j++){
                                            if(delFile[j].isFile())
                                                delFile[j].delete();//delete files
                                        }
                                        System.out.println("Files under " + strDirectoy + " have been deleted!");
                                        
                                    }
                                }else{
                                    // create the new folder
                                    boolean success = (new File(strDirectoy)).mkdir();
                                    if (success)
                                        System.out.println("Directories: " + strDirectoy + " created.");
                                    else
                                        System.out.println("Directories: " + strDirectoy + " could NOT be created.");
                                }
                                
                                

                        }catch(Exception ex){
                            System.out.println("error 1101101215::fail to " +
                                    "create folder for reporting system!");
                        }
                    }
                    break;

            // ##2.1## directoryName is a small directory Name with
            case 2: {
                        try{
                                // if existing and is a file, delete the file
                                File f = new File(strDirectoy);
                                if(f.exists()){
                                    if(f.isFile()){
                                        f.delete();
                                    }
                                }

                                // create the new folder
                                boolean success = (new File(strDirectoy)).mkdirs();
                                if (success)
                                    System.out.println("Directories: " + strDirectoy + " created.");
                                else
                                    System.out.println("Directories: " + strDirectoy + " could NOT be created.");

                        }catch(Exception ex){
                            System.out.println("error 1101101215::fail to " +
                                    "create folder for reporting system!");
                        }
                    }
                    break;
                    
            default:{
                        return -1;
                    }                    

        }

        return 1;
    }
}
